#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/7/19  12:30
# @Author: 余浪人
# @email: yulangren520@gmail.com

from wtforms import Form, StringField, TextAreaField, BooleanField, validators, DateTimeField, RadioField, DecimalField, \
    SelectField, ValidationError
from apps.lib.public import random_click
from apps.models.content_model import TL_Column_model, TL_Content_Model
from flask import request


class TL_Content_Form(Form):
    title = StringField(
        label='标题', validators=[validators.DataRequired(message='标题不可为空'), validators.Length(max=100, message='标题太长')],
        render_kw={"placeholder": "请输入标题", "style": "width: 50%;", "class": "layui-input"})
    title_color = StringField(
        label='标题颜色', render_kw={"readonly": "readonly", "style": "width: 100px;", "class": "layui-input"})
    # classify = SelectField(label='分类标签',coerce=int, validators=[validators.DataRequired('请选择标签!')])
    superior = SelectField(label='所属栏目', coerce=int, validators=[validators.DataRequired('请选择栏目!')])
    thumb_img = StringField(
        label='缩略图',
        render_kw={"autocomplete": "off", "class": "layui-input", "placeholder": "http://",
                   "style": "display: inline-block;width: 50%;"})
    keyword = StringField(
        label='关键词', validators=[validators.Length(max=255, message='关键词过多')],
        render_kw={"placeholder": "请输入关键词", "autocomplete": "off", "class": "layui-input", "style": "width:70%;"})
    description = TextAreaField(
        label='描述', render_kw={"class": "layui-textarea", "style": "width:70%;min-height:38px"})
    permissions = RadioField(
        label='可见性', choices=[(0, "免费"), (1, "付费")], render_kw={"lay-verify": "required", "style": "width:100%;", "lay-filter": "permissions"}, coerce=int, default=0)
    reward = DecimalField(label='打赏金额', validators=[validators.Optional(True)],
                          render_kw={"placeholder": "请输入金额", "autocomplete": "off", "class": "layui-input", "style": "width:10%;min-width: 100px;", "oninput": "this.value=this.value.replace(/[^0-9\.]/g,'')"})
    content = TextAreaField(
        '内容', render_kw={"class": "layui-textarea", "rows": "10", "style": "width:80%"})
    pay_content = TextAreaField(
        '付费内容', render_kw={"class": "layui-textarea", "rows": "10", "style": "width:80%"})
    download = StringField(
        label='下载地址', render_kw={"placeholder": "请输入下载地址", "class": "layui-input", "autocomplete": "off", "style": "width:70%"})
    author = StringField(
        label='作者', default="佚名", validators=[validators.Length(max=64, message='作者过长!')],
        render_kw={"autocomplete": "off", "class": "layui-input", "style": "width: 200px"})
    contributor = StringField(
        label='投稿者', render_kw={"autocomplete": "off", "class": "layui-input", "style": "width: 200px"})
    clicks = StringField(label='点击数', default=random_click, validators=[validators.DataRequired('点击数不可为空!'), validators.Regexp(r'^\d*$', message='点击数格式为数字!')],
                         render_kw={"class": "layui-input", "style": "width: 108px;float: left;"})
    update_time = DateTimeField(label='发布时间', validators=[validators.DataRequired('请填写发布时间!')],
                            render_kw={"class": "layui-input", "style": "width: 200px"})
    is_focus = BooleanField(
        label='', render_kw={"title": "焦点图", "lay-skin": "primary", "style": "width: 50px;"})
    is_roll = BooleanField(
        label='', render_kw={"title": "滚动图", "lay-skin": "primary", "style": "width: 50px;"})
    is_roll_text = BooleanField(
        label='', render_kw={"title": "滚动文字", "lay-skin": "primary", "style": "width: 50px;"})
    is_index_stick = BooleanField(
        label='', render_kw={"title": "首页置顶", "lay-skin": "primary", "style": "width: 50px;"})
    is_list_stick = BooleanField(
        label='', render_kw={"title": "列表置顶", "lay-skin": "primary", "style": "width: 50px;"})
    is_recommend = BooleanField(
        label='', render_kw={"title": "推荐", "lay-skin": "primary", "style": "width: 50px;"})
    status = BooleanField(
        label='', render_kw={"title": "是否通过", "lay-skin": "primary", "checked": "", "style": "width: 50px;"})

    def __init__(self, *args, **kwargs):
        super(TL_Content_Form, self).__init__(*args, **kwargs)
        if not TL_Column_model.query.count():
            self.superior.choices = [(0, '请先添加分类')]
        else:
            self.superior.choices = [(r.id, r.column_name) for r in TL_Column_model.query.all()]  # 初始化所属栏目

    def validate_title(self, title):
        try:
            raw_id = int(request.url.split('_')[-1])
        except:
            raw_id = 0
        con_obj = TL_Content_Model.query.filter_by(title=title.data).first()
        if con_obj and not con_obj.id == raw_id:
            raise ValidationError("内容标题已存在!")


# 分类form
class TL_Column_Form(Form):
    def __init__(self, *args, **kwargs):
        super(TL_Column_Form, self).__init__(*args, **kwargs)
        self.column_parent_id.choices = [(r.id, r.column_name) for r in TL_Column_model.query.all()]  # 初始化所属分类
        self.column_parent_id.choices.insert(0, (0, '顶级分类'))

    column_name = StringField('分类名称',
                              validators=[validators.DataRequired('名称不可为空!')],
                              render_kw={"required": "", "class": "layui-input", "placeholder": '请输入名称'})
    column_parent_id = SelectField('上级分类', coerce=int)
    column_order = StringField('分类排序',
                               validators=[validators.Regexp(r'^\d*$', message='分类排序格式有误!')], default=0, render_kw={"class": "layui-input", "onkeyup": "value=value.replace(/[^\d]/g,'')"})
    column_keyword = TextAreaField('关键词', validators=[validators.Length(max=1024, message='关键词过长!')],
                                   render_kw={"class": "layui-textarea"})
    column_description = TextAreaField('分类描述', validators=[validators.Length(max=1024, message='描述内容过长!')],
                                       render_kw={"class": "layui-textarea"})
    column_link = StringField('跳转链接', validators=[validators.Optional(True), validators.URL(message='链接格式不正确!')],
                              render_kw={"class": "layui-input", "style": 'width: 300px;', "placeholder": 'http://'})
